package com.bgIntlgn.client.entity.jishubu.papaerLibary;

import java.util.Date;

import yuhuibear.poat.annotations.action.AutoGenerateValue;
import yuhuibear.poat.annotations.action.Macro;
import yuhuibear.poat.annotations.relation.Field;
import yuhuibear.poat.annotations.relation.Table;
import yuhuibear.poat.object.bean.Bean;

@Table(name = "borrow_return_records", constrain = "")
public class BorrowReturnRecord implements Bean {

	/**
	 * id
	 */
	@AutoGenerateValue(skipOnNull = true, sqlToGet = "select currval('borrow_return_records_id_seq')")
	@Field(name = "id", type = "bigserial", constrain = "primary key")
	private Long id;

	@Field(name = "suite_id", type = "bigserial", constrain = "not null references pic_paper_suites(id)")
	private Long suiteId;

	/**
	 * 经手份数， 正为入， 负为借出。
	 */
	@Field(name = "copies", type = "int", constrain = "")
	private Integer copies;

	/**
	 * 损毁
	 */
	@Field(name = "broken", type = "boolean", constrain = "default false")
	private boolean broken;

	/**
	 * 经手时间
	 */
	@Field(name = "time", type = "timestamp", constrain = "")
	private Date time;

	/**
	 * 借出员工
	 */
	@Field(name = "borrower_id", type = "int8", constrain = "references employees(id) on update cascade ")
	private Long borrowerId;

	/**
	 * 负责人。
	 */
	@Field(name = "charger_id", type = "int8", constrain = "not null references employees(id) on update cascade ")
	private Long chargerId;

	/**
	 * 备注
	 */
	@Field(name = "comment", type = "varchar(250)", constrain = "")
	private String comment;

	@Macro(name = "sum_picsuite_quantity_trigger", depend = "")
	public String quantityTrigger() {
		String sql = "create or replace function sum_picsuite_quantity_trigger() returns trigger as $BODY$ "
				+ "declare "
				+ "	q int; "
				+ "begin "
				+ "	select sum(br.copies) into q from borrow_return_records as br where NEW.suite_id = br.suite_id and broken = false;"
				+ "	update pic_paper_suites set in_lib = ( q = 0) where id = NEW.suite_id;"
				+ "	return NEW; " + "end;$BODY$ language 'plpgsql';";
		sql = sql
				+ "create trigger trigger_picpaper_quantity after insert or update on borrow_return_records for each row execute procedure sum_picsuite_quantity_trigger();";
		return sql;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getCopies() {
		return this.copies;
	}

	public void setCopies(Integer copies) {
		this.copies = copies;
	}

	public Date getTime() {
		return this.time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public Long getBorrowerId() {
		return this.borrowerId;
	}

	public void setBorrowerId(Long borrowerId) {
		this.borrowerId = borrowerId;
	}

	public String getComment() {
		return this.comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getChargerId() {
		return chargerId;
	}

	public void setChargerId(Long chargerId) {
		this.chargerId = chargerId;
	}

	public boolean isBroken() {
		return broken;
	}

	public void setBroken(boolean broken) {
		this.broken = broken;
	}

	public Long getSuiteId() {
		return suiteId;
	}

	public void setSuiteId(Long suiteId) {
		this.suiteId = suiteId;
	}

}
